package cn.jtfadmin.base.lang.common.domain.entity;


import cn.jtfadmin.base.lang.common.event.EntityEvent;
import cn.jtfadmin.base.lang.enums.EntityEventEnum;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * id实体监听器<br/>
 * 当保存时设置id
 *
 * @author jtf
 * @since 0.0.1
 * @see EntityListener
 * @see EntityIdGen
 */
@Component
public class EventPushEntityListener implements EntityListener, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void prePersist(Object object) {
        applicationContext.publishEvent(new EntityEvent(object, EntityEventEnum.PRE_PERSIST));
    }

    @Override
    public void preUpdate(Object object) {
        applicationContext.publishEvent(new EntityEvent(object, EntityEventEnum.PRE_UPDATE));
    }

    @Override
    public void preRemove(Object object) {
        applicationContext.publishEvent(new EntityEvent(object, EntityEventEnum.PRE_REMOVE));
    }

    @Override
    public void postPersist(Object object) {
        applicationContext.publishEvent(new EntityEvent(object, EntityEventEnum.POST_PERSIST));
    }

    @Override
    public void postUpdate(Object object) {
        applicationContext.publishEvent(new EntityEvent(object, EntityEventEnum.POST_UPDATE));
    }

    @Override
    public void postRemove(Object object) {
        applicationContext.publishEvent(new EntityEvent(object, EntityEventEnum.POST_REMOVE));
    }

    @Override
    public void postLoad(Object object) {
        applicationContext.publishEvent(new EntityEvent(object, EntityEventEnum.POST_LOAD));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
